﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace DownloadMag.Core
{
    class ProtocolProviderFactory
    {
        private static Hashtable protocolHandlers = new Hashtable();

        public static void RegisterProtocolHandler(string uriScheme, Type protocolProvider)
        {
            protocolHandlers[uriScheme] = protocolProvider;
        }

        public static IProtocolProvider GetProvider(string url)
        {
            return InternalGetProvider(url);
        }

        public static Type GetProviderType(string url)
        {
            Uri uri = new Uri(url);
            Type type = protocolHandlers[uri.Scheme] as Type;
            return type;
        }
        private static IProtocolProvider InternalGetProvider(string url)
        {
            Type type = GetProviderType(url);
            IProtocolProvider provider = CreateFromType(type);
            return provider;
        }
        private static IProtocolProvider CreateFromType(Type type)
        {
            IProtocolProvider provider = (IProtocolProvider)Activator.CreateInstance(type);
            return provider;
        }
    }
}
